﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;
using HsTec.Mobile.Green.SqlServerDAL;

public partial class DocumentManage_Down_Document : System.Web.UI.Page
{
    DataFolder MyFolder = new DataFolder();
    DataFile MyDataFile = new DataFile();
    BaimasoftFile MyFile = new BaimasoftFile();

    LogManage MyLog = new LogManage();

    static public ArrayList hif = new ArrayList(); //保存文件列表

    public string m_Root = ConfigurationManager.AppSettings["DocumentMange"];
    public string sSwitchPath = ConfigurationManager.AppSettings["DocumentSwitchPath"];
    public string sEncryptFile = ConfigurationManager.AppSettings["EncryptFile"];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            try
            {
                //string sFolderID   = Request.QueryString["FolderID"].ToString().Trim(); //选择文件夹ID
                //string sChoicePath = MyFolder.GetFileMovePath(sFolderID);               //选择路径

                string sChoiceIDCol = Request.QueryString["ChoiceIDCol"].ToString().Trim(); //选择文件ID集合
                string[] aCholceID = sChoiceIDCol.Split('|');
                int iChoiceID = aCholceID.Length;
                for (int i = 0; i < iChoiceID; i++)
                {
                    string sFileName = "";
                    string sDownFilePath = "", sTempFilePath="";    //原文件所在路径
                    string sChoicePath = "";
                    if (aCholceID[i].Length > 0)
                    {
                        DataTable MyDT = MyDataFile.GetDataFileInfo(Convert.ToInt32(aCholceID[i]));  //取各文件的相关信息；
                        foreach (DataRow dr in MyDT.Rows)
                        {
                            sChoicePath = dr["path"].ToString().Trim();
                            sFileName = dr["Name"].ToString().Trim();
                            sDownFilePath = sChoicePath + "\\" + sFileName;
                            sTempFilePath = sChoicePath + "\\" + dr["AliasName"].ToString().Trim();
                            FileList.Items.Add(sDownFilePath);
                            hif.Add(sTempFilePath);          //把文件完整路径添加到数组中去
                        }

                    }
                }
            }
            catch
            {
            }
        }
    }

    /// <summary>
    /// 删除文档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DelFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)
        {
            strStatus.Text = "<font color=red>必须指定要删除的文件.</font>";
            return;
        }
        else if (FileList.Items.Count != 0)
        {
            FileList.Items.Remove(FileList.SelectedItem.Text);
            hif.RemoveAt(FileList.SelectedIndex);
            strStatus.Text = "";
        }
    }

    /// <summary>
    /// 清空文档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ClearFile_Click(object sender, EventArgs e)
    {
        FileList.Items.Clear();
        hif.Clear();
        strStatus.Text = "";
    }

    /// <summary>
    /// 添加文件夹路径
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AddFolderPath_Click(object sender, EventArgs e)
    {
        if (FindFolder.HasFile)  //判断浏览文本框中有没有文件内容
        {
            string sfileFullName = FindFolder.PostedFile.FileName;
            int ifileFullName = sfileFullName.Length;
            int ifilename = System.IO.Path.GetFileName(sfileFullName).Length + 1;
            string sfilepath = sfileFullName.Remove(ifileFullName - ifilename, ifilename);

            DownToPath.Value = sfilepath; //  System.IO.Path.GetDirectoryName(FindFolder.PostedFile.FileName);
        }
        else
        {
            strStatus.Text = "<font color=red>必须要有上传的文件夹.</font>";
        }
    }

    /// <summary>
    /// 批量下载文档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        int icount = 0;
        string sDownToPath = DownToPath.Value.ToString().Trim();  //服务器文档路径
        int iFileList = FileList.Items.Count;
        if (iFileList > 0)
        {
            if (Directory.Exists(sDownToPath) == true)
            {
                string sDownFile = "文档下载中...";
                for (int i = 0; i < iFileList; i++)
                {
                    string sTempName =  FileList.Items[i].ToString().Trim();
                    string sServerFilePath = m_Root + sTempName;
                    string sTempFilePath = m_Root + hif[i].ToString().Trim() ;
                    string sFileName = System.IO.Path.GetFileName(sTempFilePath); // 文件名
                    string sDecrypFileName = sSwitchPath + "\\" + sFileName; //解密后路径

                    string sLocalFilePath = sDownToPath + "\\" + System.IO.Path.GetFileName(sServerFilePath);  //保存到路径                    
                    try
                    {
                        MyFile.DecryptFile(sTempFilePath, sDecrypFileName, sEncryptFile); //对文件进行解密处理

                        MyFile.DownLoadServerFile(sDecrypFileName, sLocalFilePath);       //从服务器上把文件下载到本地

                        try
                        {
                            string sPerID = (string)Session["Baimasoft_Pers_ID"];
                            string sContent = "用户" + (string)Session["Baimasoft_User_LogonID"] + "从服务器上把文件：" + sDecrypFileName + "下载到本地：" + sLocalFilePath;
                            string sIP = Page.Request.UserHostAddress;
                            MyLog.InsertLog("3", sPerID, sContent, sIP);
                        }
                        catch
                        {
                            string sPerID = "0";
                            string sContent = "从服务器上把文件：" + sDecrypFileName + "下载到本地：" + sLocalFilePath;
                            string sIP = Page.Request.UserHostAddress;
                            MyLog.InsertLog("3", sPerID, sContent, sIP);
                        }

                        if (File .Exists (sDecrypFileName) == true)
                        {
                            MyFile.DeleteFile(sDecrypFileName);
                        }
                        icount++;
                    }
                    catch
                    {
                    }
                    //strStatus.Text = sDownFile + "成功下载了" + icount + "个文档,共" + iFileList + "个文档！";
                }

                string sLeave = "";
                if (icount > 0)
                {
                    if (iFileList == icount)
                    {
                        sLeave = "文档下载成功！";
                    }
                    else
                    {
                        sLeave = "部分文档超载成功！";
                    }
                }
                else
                {
                    sLeave = "文件下载失败！";
                }
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + sLeave + "');window.close();</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('下载路径不正确，建议您用‘浏览...’的方式选择文件夹！注意：此功能只适用于服务器之间下载文件！');</script>");
                return;
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有可下载得文档！');</script>");
            return;
        }
    }
}
